﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConnectBasic.Shell.Hosting;
using System.IO;
using ConnectBasic.Shell.Extensibility;

namespace ConnectBasicShellSample
{
    public partial class MainForm : Form
    {
        IShellHost mHost;

        public MainForm()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

            // First time?
            if (mHost == null)
            {                                

                // Initialize the shell
                mHost = ShellFactory.CreateHost();

                // Open/create a macro
                IProjectStore projectStore;
                IProject project;

                if (OpenSaveMacro.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                if (File.Exists(OpenSaveMacro.FileName))
                {

                    // Open the existing project
                    projectStore = new OpenXmlFileProjectStore(OpenSaveMacro.FileName, System.IO.FileMode.Open);
                    project = mHost.Projects.LoadProject(projectStore);
                }
                else
                {
                    // Create the project we're going to load into the host
                    projectStore = new OpenXmlFileProjectStore(OpenSaveMacro.FileName, System.IO.FileMode.CreateNew);
                    project = mHost.Projects.LoadProject(projectStore);

                    // Add a standard module by default
                    project.Items.Add("Module1", WellKnownItemTypeNames.StandardModule);
                }
            }

            // Show the shell
            mHost.Shell.Show();
        }
    }
}
